
"""
@名称：练习
@作者：Fida1214
@日期：2025/4/9
"""

import random

class MagicStudent:
    def __init__(self, name, magic_power, spell_list):
        self.name = name
        self.magic_power = magic_power
        self.spell_list = spell_list
        self.health = 100
        self.silenced = False  # 是否被沉默
        self.stunned = False  # 是否被眩晕

    def cast_spell(self, other_student, students):
        if self.silenced:
            print(f"{self.name} 被沉默了，无法施法！")
            self.silenced = False
            return
        if self.stunned:
            print(f"{self.name} 被眩晕了，无法行动！")
            self.stunned = False
            return

        spell = random.choice(self.spell_list)
        if spell == "护盾咒":
            # 为自身恢复生命值
            heal_power = self.magic_power * random.uniform(0.8, 1.2)
            self.health += heal_power
            print(f"{self.name} 使用了护盾咒，恢复了 {heal_power:.2f} 点生命值！")
            print(f"{self.name} 当前生命值为 {self.health:.2f}。")
        elif spell == "治疗咒":
            # 为随机一个存活的队友恢复生命值
            allies = [student for student in students if student.health > 0 and student != self]
            if allies:
                ally = random.choice(allies)
                heal_power = self.magic_power * random.uniform(0.8, 1.2)
                ally.health += heal_power
                print(f"{self.name} 使用了治疗咒，为 {ally.name} 恢复了 {heal_power:.2f} 点生命值！")
                print(f"{ally.name} 当前生命值为 {ally.health:.2f}。")
        elif spell == "沉默咒":
            # 使目标在下一轮无法施法
            print(f"{self.name} 对 {other_student.name} 施放了沉默咒！")
            other_student.silenced = True
        elif spell == "眩晕咒":
            # 使目标在下一轮无法行动
            print(f"{self.name} 对 {other_student.name} 施放了眩晕咒！")
            other_student.stunned = True
        elif spell == "群体攻击咒":
            # 对所有敌人造成少量伤害
            print(f"{self.name} 施放了群体攻击咒！")
            for student in students:
                if student != self and student.health > 0:
                    spell_power = self.magic_power * random.uniform(0.3, 0.5)
                    student.health -= spell_power
                    print(f"{self.name} 对 {student.name} 造成了 {spell_power:.2f} 点群体伤害！")
                    if student.health <= 0:
                        print(f"{student.name} 被打败了！")
                    else:
                        print(f"{student.name} 还剩下 {student.health:.2f} 点生命值。")
        elif spell == "反弹咒":
            # 将下一次受到的伤害反弹给攻击者
            print(f"{self.name} 施放了反弹咒！")
            self.shield = True
        else:
            # 普通攻击咒语
            spell_power = self.magic_power * random.uniform(0.8, 1.2)
            other_student.health -= spell_power
            print(f"{self.name} 对 {other_student.name} 施展了 {spell}，造成了 {spell_power:.2f} 点伤害！")
            if other_student.health <= 0:
                print(f"{other_student.name} 被打败了！")
            else:
                print(f"{other_student.name} 还剩下 {other_student.health:.2f} 点生命值。")

def magic_battle(students):
    print("魔法对决开始！")
    while len([student for student in students if student.health > 0]) > 1:
        # 随机选择一个存活的学生
        attacker = random.choice([student for student in students if student.health > 0])
        # 随机选择一个目标，但不能是自己
        targets = [student for student in students if student.health > 0 and student != attacker]
        if targets:
            target = random.choice(targets)
            attacker.cast_spell(target, students)
    winner = [student for student in students if student.health > 0][0]
    print(f"魔法对决结束，胜利者是 {winner.name}！")

# 创建学生实例
student1 = MagicStudent("student1", 25, ["火焰咒", "冰冻咒", "闪电咒", "护盾咒", "治疗咒", "沉默咒", "眩晕咒", "群体攻击咒", "反弹咒"])
student2 = MagicStudent("student2", 25, ["火焰咒", "变形咒", "护盾咒", "治疗咒", "沉默咒", "眩晕咒", "群体攻击咒", "反弹咒"])
student3 = MagicStudent("student3", 25, ["火焰咒", "变形咒", "护盾咒", "治疗咒", "沉默咒", "眩晕咒", "群体攻击咒", "反弹咒"])
student4 = MagicStudent("student4", 25, ["火焰咒", "变形咒", "护盾咒", "治疗咒", "沉默咒", "眩晕咒", "群体攻击咒", "反弹咒"])
student5 = MagicStudent("student5", 25, ["火焰咒", "变形咒", "护盾咒", "治疗咒", "沉默咒", "眩晕咒", "群体攻击咒", "反弹咒"])
student6 = MagicStudent("student6", 25, ["火焰咒", "变形咒", "护盾咒", "治疗咒", "沉默咒", "眩晕咒", "群体攻击咒", "反弹咒"])
student7 = MagicStudent("student7", 25, ["火焰咒", "变形咒", "护盾咒", "治疗咒", "沉默咒", "眩晕咒", "群体攻击咒", "反弹咒"])
student8 = MagicStudent("student8", 25, ["火焰咒", "变形咒", "护盾咒", "治疗咒", "沉默咒", "眩晕咒", "群体攻击咒", "反弹咒"])

# 将所有学生放入一个列表
students = [student1, student2, student3, student4, student5, student6, student7, student8]

# 开始魔法对决
magic_battle(students)